<template>
<div class="container">
  <el-card class="box-card login" >
    <el-input v-model="userName" placeholder="账号" style="margin-bottom: 20px;margin-top: 50px" prefix-icon="el-icon-user"></el-input>
    <el-input show-password v-model="userPassword" placeholder="密码" style="margin-top: 10px;margin-bottom: 20px" prefix-icon="el-icon-lock" ></el-input>
    <el-button type="primary" style="width: 100%" @click="userLogin">点击登录</el-button>

  </el-card>
</div>
</template>

<script>
import axios from "axios";

export default {
  name: "login",
  data(){
    return{
      userName:"",
      userPassword:"",
      loading:null
    }
  },
  mounted() {
    this.$bus.$on("cancelLoading",()=>{
      this.loading.close();
    })
  },
  methods:{
    //用户登录
    userLogin(){
      if (this.userName==null||this.userName==''){
        this.$message({message:"用户名不能为空",type:"error"})
        return false;
      }
      if (this.userPassword==null||this.userPassword==''){
        this.$message({message:"密码不能为空",type:"error"})
        return false;
      }
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      //通过数据校验进行用户名密码判断
      axios({
        url:'http://localhost:8080/api/user/login',
        method:'post',
        data:{
          "userAccount":this.userName,
          "userPassword":this.userPassword
        }
      }).then(resp=>{
        if (resp.data.code==200){
          this.$bus.$emit('isLogin')
          loading.close();
          this.$message({
            message:resp.data.msg,
            type:'success'
          })
          //将数据存入localStorage
          window.localStorage.setItem("token",resp.data.data)
        }
        else this.$message({
          message:resp.data.msg,
          type:"error"
        })
      })
    }

  }
}
</script>

<style scoped>
body{
  background-image: url("./img/background.jpg");
  background-size: 100%;
  background-repeat: no-repeat;
}
.container{
  width: 100%;
  height: 700px;
  background-image: url("./img/background.jpg");
  background-size: 100%;
  background-repeat: no-repeat;
  overflow: hidden;
}
.login{
  width: 450px;
  height: 300px;
  margin: 120px auto;
  box-shadow: 0px 0px 5px rgba(0,0,0,.4);
}
</style>